package com.example.demo.service;

import com.example.demo.entity.GoodsItem;
import org.springframework.stereotype.Service;

import java.util.*;
import java.lang.Long;

/**
 *
 */
@Service
public class GoodServer {

    //创建线程安全的Map，模拟Goods的信息存储
    private Map<Long, GoodsItem> goodss = Collections.synchronizedMap(new HashMap<Long, GoodsItem>());
    public GoodsItem addGoods(GoodsItem goods){
        goodss.put(goods.getId(),goods);
        return goods;
    }

    public GoodsItem getGoods(long id){return goodss.get(id);}

    public List<GoodsItem> findGoodss(String name,Double price){
        List<GoodsItem> result = new ArrayList<>();
        for(GoodsItem goods: goodss.values()){
            if(name != null && !goods.getName().contains(name)){
                continue;
            }
            if(price != null && !goods.getPrice().equals(price)){
                continue;
            }
            result.add(goods);
        }
        return result;
    }

    public void updataGoods(long id,GoodsItem goods){
        GoodsItem goods2 = goodss.get(id);
        goods2.setName(goods.getName());
        goods2.setPrice(goods.getPrice());
        goodss.put(id,goods2);
    }

    public void deleteGood(long id){goodss.remove(id);}
}
